package org.sixtynine.stock.service.file;

import java.io.File;
import java.util.List;

import org.sixtynine.stock.Constants;
import org.sixtynine.stock.ReturnData;
import org.sixtynine.stock.entity.BaseEntity;
import org.sixtynine.stock.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

public class FileDataServiceImpl implements FileDataService {

	@Autowired
	ExcelProcessorFactory excelProcessorFactory;

	@Autowired
	GenericService genericService;

	private String fileBaseDir;
	private String companyDocDir;
	private String bannerLocation;
	private String companyDocLocation;

	public static final String COMPANY_DOCUMENT = "COMPANY_DOCUMENT";

	@Override
	public void ping() {
		// System.out.println("Ping......... : " + fileBaseDir);
	}

	@Override
	public ReturnData<List<? extends BaseEntity>> process(String fileName,
			String type) {
		ReturnData<List<? extends BaseEntity>> returnData = excelProcessorFactory
				.getExcelProcessor(type).process(fileBaseDir + fileName);
		if (returnData._isSuccess()) {
			// save the list
			for (BaseEntity baseEntity : returnData.getData()) {
				genericService.saveOrUpdate(baseEntity);
			}
			// genericService.saveList((List<BaseEntity>)returnData.getData());
		}

		// delete the file after processing
		deleteFile(fileName);

		return returnData;
	}

	public void setFileBaseDir(String fileBaseDir) {
		this.fileBaseDir = fileBaseDir;
	}

	@Override
	public ReturnData<String> saveFile(MultipartFile multipartFile,
			String type, String fileName) {

		ReturnData<String> returnData = new ReturnData<String>(
				Constants.SUCCESS, "");

		if (multipartFile == null) {
			returnData.setNo(Constants.ERROR);
			returnData.setMsg("File is null");
			return returnData;
		}

		String destination = fileBaseDir + multipartFile.getOriginalFilename();
		if (fileName != null) {
			if (type.equals(COMPANY_DOCUMENT)) {
				destination = companyDocDir + fileName;
			}
		}

		try {
			multipartFile.transferTo(new File(destination));
		} catch (Exception e) {
			e.printStackTrace();
			returnData.setNo(Constants.ERROR);
			returnData.setMsg(e.getMessage());
		}

		return returnData;
	}

	@Override
	public ReturnData<String> deleteFile(String fileName) {
		File file = new File(fileBaseDir + fileName);

		ReturnData<String> returnData = new ReturnData<String>(
				Constants.SUCCESS, "File successfully deleted");

		boolean success = false;
		try {
			success = file.delete();
		} catch (Exception e) {
			returnData.setNo(Constants.ERROR);
			returnData.setMsg(e.getMessage());
		}

		if (!success) {
			returnData.setNo(Constants.ERROR);
		}

		return returnData;
	}

	public String getCompanyDocDir() {
		return companyDocDir;
	}

	public void setCompanyDocDir(String companyDocDir) {
		this.companyDocDir = companyDocDir;
	}

	public String getBannerLocation() {
		return bannerLocation;
	}

	public void setBannerLocation(String bannerLocation) {
		this.bannerLocation = bannerLocation;
	}

	public String getCompanyDocLocation() {
		return companyDocLocation;
	}

	public void setCompanyDocLocation(String companyDocLocation) {
		this.companyDocLocation = companyDocLocation;
	}

}
